const axios = require("axios");
const { getItem } = require("@/utils/localStorageOpe");
const { Message } = require("element-ui");

const instance = axios.create({
    baseURL: "http://localhost:3000/api",
    // baseURL: "/api",
    timeout: 3000,
});

instance.interceptors.request.use(
    (config) => {
        // console.log(config);
        // 判断是否存在token，如果存在则放进Authorization中
        let token = getItem("token");
        if (token) {
            config.headers["Authorization"] = token;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    (response) => {
        const { data } = response;
        if (data.status !== 200) {
            Message({
                message: data.message || "error",
                type: "warning",
            });
        }
        return response;
    },
    (error) => {
        return Promise.reject(error);
    }
);

module.exports = instance;